﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Temp_20190707_AForm
{
    public partial class DockTipPanel : UserControl
    {
        [Obsolete("必须初始化 DockPanel 对象", true)]
        public DockTipPanel()
        {
            InitializeComponent();
        }
        public DockTipPanel(DockPanel dockPanel)
        {
            InitializeComponent();
            this.DockPanel = dockPanel;
            m_Timer = new Timer();
            m_Timer.Interval = 100;
            m_Timer.Tick+=new EventHandler(Timer_Tick);
            m_Timer.Enabled = true;
        }


        private Timer m_Timer = new Timer();
        public DockPanel DockPanel { get; set; }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.Refresh();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            var g = e.Graphics;
            var dockPanel = this.DockPanel;
            if (dockPanel == null || !dockPanel.IsHandleCreated || dockPanel.Disposing || dockPanel.IsDisposed) return;

            var layoutInfo = dockPanel.GetLayoutInfo();
            var content = layoutInfo.GetContent();
            var tabGroups = layoutInfo.GetTabGroups();

            if (content != null)
            {
                DrawDockContent(g, content);
            }
            if (tabGroups != null)
                foreach (var group in tabGroups)
                {
                    DrawDockTabGroup(g, group);
                }
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            this.Refresh();
        }


        public DockEventArgs NewDockEventArgs()
        {
            Point point1 = Control.MousePosition;
            Point point2 = DockPanel.PointToClient(point1);

            var layoutInfo = DockPanel.GetLayoutInfo();
            var content = layoutInfo.GetContent();
            var tabGroups = layoutInfo.GetTabGroups();

            if (content.TipRect.Contains(point2))
            {
                //MessageBox.Show("content");
                return new DockEventArgs {Control = content.Control, TipLayout = content};
            }
            foreach (var tabGroup in tabGroups)
            {
                if (tabGroup.TipRect.Contains(point2))
                {
                    //MessageBox.Show("tabGroup");
                    return new DockEventArgs { Control = tabGroup.Control, TipLayout = tabGroup };
                }
            }


            return null;
        }


        public void DrawDockContent(Graphics g, DockLayoutInfo layout)
        {
            Color color = Color.FromArgb(200, Color.Teal);
            if (layout.MouseInTipRect(DockPanel)) color = Color.FromArgb(200, Color.Thistle);
            g.FillRectangle(new SolidBrush(color), layout.TipRect);
        }
        public void DrawDockTabGroup(Graphics g, DockLayoutInfo layout)
        {
            Color color = Color.FromArgb(200, Color.Tan);
            if (layout.MouseInTipRect(DockPanel)) color = Color.FromArgb(200, Color.Tomato);
            g.FillRectangle(new SolidBrush(color), layout.TipRect);
        }


    }
}
